import React, { Component } from 'react'
// 创建context对象
const MyContext=React.createContext()
const {Provider}=MyContext
export default class A extends Component {
    state={username:'tom'}
    render() {
        const {username}=this.state
        return (
            <div>
                <h3>我是A组件</h3>
                <h4>我的用户名是{username}</h4>
                {/* <B username={this.state.username}></B> */}
                <Provider value={username}><B></B></Provider>
            </div>
        )
    }
}
class B extends Component {
    render() {
        // const {username}=this.state
        
        return (
            <div>
                <h3>我是B组件</h3>
                <h4>我从A组件收到用户名是??</h4>
                {/* <C username={this.props.username}></C> */}
                <C></C>
            </div>
        )
    }
}
class C extends Component {
    // 申明使用
    static contextType=MyContext
    render() {
        return (
            <div>
                <h3>我是C组件</h3>
                <h4>我从A组件收到用户名是{this.context}</h4>
            </div>
        )
    }
}
